<style scoped>
  .main{
    /*padding-bottom: 70px;*/
  }
  .income{
    height:100%;
    background: #edf1f5;
    /*margin-top: -8px;*/
  }
  .nav ul{
    display: flex;
    flex-direction: row;
    padding:8px 0;
    background: #ffffff;
    border-bottom: 1px solid #e9e9e9;
  }
  .nav ul li{
    flex: 1;
  }
  .nav ul li a{
    display: flex;
    flex-direction: row;
    flex: 1;
    justify-content: center;
    align-items: center;
  }
  .nav ul li p{
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    position: relative;
  }
  .nav ul li:nth-child(2) p{
    color: #4D63EB;
  }
  .nav ul li:nth-child(2) p b{
    width:50%;
    height:3px;
    background: -webkit-linear-gradient(right, #91A5F8, #6379F8);
    position: absolute;
    left:25%;
    bottom:-8px;
  }
  .nav ul li h4{
    color: #e9e9e9;
  }
  .weight-top{
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 6px 10px;
    border-bottom: 1px solid #e9e9e9;
  }
  .weight-top div{
    display: flex;
    flex-direction: row;
    align-items:center;
    justify-content: center;
  }
  .weight-top div h4{
    opacity: 0.5;
  }
  .weight-top div h3{
    opacity: 0.5;
    margin-left: 4px;
  }

  .content{
    display: flex;
    flex-direction: column;
  }
  .buy{
    display: flex;
    flex-direction: column;
    background: #ffffff;
  }
  .buy-top{
    border:1px solid #e9e9e9;
    padding: 6px 10px;
  }
  .buy-bottom>ul{
    display: flex;
    flex-direction: column;
  }
  .buy-bottom>ul>li>a{
    border-bottom: 1px solid #e9e9e9;
    padding: 6px 20px 6px 20px;
    height:100%;
    display: flex;
    flex-direction: row;
    justify-content: space-between;
  }
  .buy-bottom>ul>li>a>.left>div{
    display: flex;
    flex-direction: row;
    margin-top: 4px;
  }
  .buy-bottom>ul>li>a>.left>div h4{
    border:1px solid #FE7A4A;
    color: #FE7A4A;
    border-radius: 5px;
    padding:0 4px;
  }
  .buy-bottom>ul>li>a>.left>div h4:last-child{
    margin-left: 4px;
  }
  .buy-bottom>ul>li>a>.right{
    /*display: flex;*/
    /*flex-direction: column;*/
    /*justify-content: center;*/
    /*align-items: flex-end;*/
    /*padding-right: 6px;*/
  }
  .right{
    display: flex;
    flex-direction: row;
  }
  .right>div{
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
  }
  .right>div:first-child{
    /*border-right:1px solid #e9e9e9;*/
    padding-right: 4px;
    display: flex;
    align-items: flex-end;
  }
  .right>div:first-child>h4{
    opacity: 0.5;
  }
  .right>div:last-child>p{
    background: -webkit-linear-gradient(top right,#91A5F8,#6379F8);
    padding:4px 2px;
    color: #ffffff;
    border-radius: 4px;
    margin-left: 4px;
  }
  .right>div:last-child>p a{
    color: #ffffff;
  }

  .ulchild{
    background: #edf1f5;
    display: flex;
    flex-direction: column;
  }
  .ulchild>li a{
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    padding:4px 7px 6px 40px;
  }
  .ulchild>li a>.left>div>small{
    border:1px solid #FE7A4A;
    color: #FE7A4A;
    border-radius: 5px;
    padding:0 4px;
  }
  .ulchild>li a>.right h3{
    display: flex;
    align-items: center;
  }
  .weight{
    margin-top: 6px;
    background: #ffffff;
  }
  .weight-top{
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 6px 10px;
    border-bottom: 1px solid #e9e9e9;
  }
  .weight-top div{
    display: flex;
    flex-direction: row;
    align-items:center;
    justify-content: center;
  }
  .weight-top div h4{
    opacity: 0.5;
  }
  .weight-top div h3{
    opacity: 0.5;
    margin-left: 4px;
  }
  .weight-bottom{
    display: flex;
  }
  .weight-bottom>div:first-child{
    flex: 4;
  }
  .weight-bottom>div:last-child{
    flex: 6;
  }
  .pie{
    height: 200px !important;
    overflow: hidden;
  }
  .echarts {
    height: 200px;
    width:100%;
  }
  .echart>div:first-child{
    display: flex;
    height: 200px !important;
    align-items: center;
  }
  .weight-right{
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .weight-right>ul>li>ul{
    display: flex;
    flex-direction: column;
  }
  .weight-right>ul>li{
    display: flex;
    flex-direction: row;
    align-items: center;
  }
  .weight-right>ul>li h1{
    width:12px;
    height:12px;
    border-radius: 4px;
    /*background: red;*/
    margin-right: 8px;
  }
  .weight-right>ul>li h4{
    margin-left: 10px;
  }

  .weight-right>ul>li div{
    width:50px;
    display: inline-flex;
    align-items: center;
  }
  .weight-right>ul>li div h5{
  }
  .profit2{
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
  }
  .profit2 p{
    margin-top: 10px;
  }
  .profit2 h3{
    text-align: center;
    color: #6379F8;
  }


</style>
<template>

  <div class="income">
    <div class="main">
    <v-scroller
    style="padding-top: 97px"
    height="-150px"
    v-on:reload="findData"
    v-if="isIncomeAccount"
    ref="scrollBar">
      <div class="content">
        <div class="buy">
          <div class="buy-top">
            <p>已购策略</p>
          </div>
          <div class="buy-bottom">
            <ul>
              <!--<li v-for="(value, key, index) in control">-->
                <!--<router-link v-for="($value, $key, $index) in value.group" :key="$index"  :to="{path: '../strategy',query:{'id': `${key}&${$key}&${$value.labels.country}${$value.labels.index_name}`}}" >-->
                  <!--<div class="left">-->
                    <!--<h3>{{$value.labels.country}}·{{$value.labels.index_name}}</h3>-->
                    <!--<small></small>-->
                    <!--<div >-->
                      <!--<h4>{{$value.labels.feature}}</h4>-->
                      <!--<h4>{{$value.labels.size_name}}</h4>-->
                    <!--</div>-->
                  <!--</div>-->
                  <!--<div class="right">-->
                    <!--<h1>{{$value.acc_profit | toFixed}}</h1>-->
                    <!--<h4>累计盈利</h4>-->
                  <!--</div>-->
                <!--</router-link>-->
              <!--</li>-->
              <li v-for="(value, key, index) in control">
                <a @click="showChild(value)">
                  <div class="left">
                    <h3 v-if="value.labels.name">{{value.labels.name}}</h3>
                    <h3 v-else>{{value.labels.country}}·{{value.labels.index_name}}<span v-if="value.track">(跟单)</span></h3>
                    <div v-if="!value.labels.name">
                      <h4 v-if="value.labels">{{value.labels.feature}}</h4>
                      <h4 v-if="value.labels">{{value.labels.size_name}}</h4>
                    </div>
                  </div>
                  <div class="right">
                    <div>
                      <h1>{{value.acc_profit | toFixed}}</h1>
                      <h4>累计盈利</h4>
                    </div>
                    <div>
                      <!--<p>查看详情</p>-->
                    </div>
                  </div>
                </a>
                <ul class="ulchild" v-if="value.show">
                  <li v-for="($value, $key, $index) in value.group" :key="$index">
                    <router-link :to="{path: '../strategy',query:{'id': `${key}&${$key}&${JSON.stringify($value.labels)}`}}">
                      <div class="left">
                        <h3 v-if="$value.labels.name">{{$value.labels.name}}</h3>
                        <p v-else>{{$value.labels.country}}·{{$value.labels.index_name}}</p>
                        <small></small>
                      <div v-if="!$value.labels.name">
                        <small>{{$value.labels.feature}}</small>
                        <small>{{$value.labels.size_name}}</small>
                      </div>
                      </div>
                      <div class="right">
                        <h3>{{$value.acc_profit | toFixed}}</h3>
                        <div>
                          <h3 class="iconfont icon-xiangyou1"></h3>
                        </div>
                      </div>
                    </router-link>
                  </li>
                </ul>
              </li>
            </ul>
          </div>
        </div>
        <div class="weight">
          <div class="weight-top">
            <p>策略权重</p>
          </div>
          <div class="weight-bottom">
            <div class="pie">
              <chart :options="pie" width="100%"></chart>
            </div>
            <div class="weight-right">
              <ul>
                <li v-for="(value, key, index) in control">
                  <!--<ul>-->
                    <!--<li v-for="($value, $key, $index) in value.group">-->
                      <h1 v-bind:style="{'background': value.color}"></h1>
                      <div v-if="!isLoadData">
                        <p>{{value.mv/totalNum | num}}<h5>%</h5></p>
                      </div>
                      <div v-else>
                        <p>0<h5>%</h5></p>
                      </div>
                      <h4 v-if="value.labels.name">{{value.labels.name}}</h4>
                      <h4 v-else>{{value.labels.country}}{{value.labels.index_name}}</h4>
                    <!--</li>-->
                  <!--</ul>-->
                </li>
              </ul>
            </div>
          </div>
        </div>
      </div>
      </v-scroller>
      <div class="profit2" style="padding-top: 180px" v-else-if="isAccount">
        <img src="../../assets/img/sad.png" alt="" width="83" height="95">
        <div>
          <p>没开户就没有收益哦~</p>
          <h3 @click="toOpen">马上去开户</h3>
        </div>
      </div>
      <div class="profit2" style="padding-top: 180px" v-else-if="isIncome">
        <img src="../../assets/img/sad.png" alt="" width="83" height="95">
        <div>
          <p>没交易就没有收益哦~</p>
          <h3 @click="toTrade">马上去交易</h3>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import host from '../../config.js'
  import axios from 'axios'
  import ECharts from 'vue-echarts/components/ECharts.vue'
  //  import 'echarts/lib/echarts'
   import 'echarts/lib/chart/pie'

  export default {
    components: {
      chart: ECharts
    },
    data () {
      return {
        isIncomeAccount: null,
        //        是否开户
        isAccount: null,
//        是否交易
        isIncome: null,
        //        策略监控
        control: null,
        pie: null,
        totalNum: null,
        allColor: ['#ff8359','#54d169','#029cf5','#1270e3','#8739e5','#f5317f','#ffdf40','#aff57a','#46eeaa','#15eded','#59c2ff','#5496ff','#ff63de','#ff7c6e'],
        colors: null,
        isShow: true,
        isLoadData: false
      }
    },
    created () {
      this.findData()
    },
    beforeRouteEnter (to, from, next) {
      next((vm) => {
        vm.$refs.scrollBar.$refs.scrollBar.donePulldown()
      })
    },
    methods: {
      findData () {
        let that = this
        axios({
          method: 'get',
          headers: {'Authorization': window.localStorage.getItem('token')},
          url: host.host + "/api/trade/capital"
        }).then(function (data) {
          if(data.data.code === 6006) {
            that.$router.push('../login')
          }
          if(data.data.code === 6007){
            that.$refreshToken()
          }
          if(data.data.code === 200) {
            let getData = data.data.data
            if(getData.total_position){
              that.isIncomeAccount = true
            }
          }else{
            that.hasAccount()
          }
        })

        this.controlData ()
      },
//      渲染数据
      controlData () {
        var colors = ['#ff8359','#54d169','#029cf5','#1270e3','#8739e5','#f5317f','#ffdf40','#aff57a','#46eeaa','#15eded','#59c2ff','#5496ff','#ff63de','#ff7c6e']
        let that = this
        var xdata = []
        this.isLoadData = true
        this.pie = {
          tooltip: {
            show: false
          },
          series: [
            {
//            name:'访问来源',
              type:'pie',
              radius: ['50%', '70%'],
              hoverAnimation:false,
              label: {
                normal: {
                  show: false
                },
                emphasis: {
                  show: false
                }
              },
              labelLine: {
                normal: {
                  show: false
                }
              },
              data:xdata
            }
          ],
          color: colors
        };
        that.totalNum  = 0
        axios({
          method: 'GET',
          headers: {'Authorization': window.localStorage.getItem('token')},
          url: host.host + "/api/trade/strategyMonitor"
        }).then(function (data) {
          if(data.data.code === 6006) {
            that.$router.push('../login')
          }
          if(data.data.code === 6007){
            that.$refreshToken()
          }
          if(data.data.code === 200) {
            that.control = data.data.data
            var count = 0
            for(let i in that.control) {
                  if(count >= 13){
                    count = 0
                  }
                xdata.push(that.control[i].mv)
                that.totalNum += that.control[i].mv
                that.$set(that.control[i],'color',colors[count])
                count++
                if(count >= 13){
                  count = 0
                }

//              }
            }
            if(that.totalNum === 0) {
              that.totalNum = 1
            }
            that.$nextTick(() => {
              that.$refs.scrollBar.$refs.scrollBar.reset()
            })
            that.isLoadData = false
          }
        })
      },
      toOpen () {
        this.$router.push('../open2')
      },
      toTrade () {
        this.$router.push('../all')
      },
      hasAccount() {
        let that = this
        axios({
          method: 'GET',
          headers: {'Authorization': window.localStorage.getItem('token')},
          url: host.host + "/api/trade/iBind"
        }).then(function (data) {
          if(data.data.code === 6006) {
            that.$router.push('../login')
          }
          if(data.data.code === 6007){
            that.$refreshToken()
          }
          if(data.data.code === 6008) {
            that.isAccount = true
          } else if (data.data.code === 200) {
            if(data.data.data.account){
              that.isIncome = true
            }
          }
        })
      },
      showChild (_index) {
//        for(let i in this.control){
//          this.$set(this.control[i],'show',false)
//        }
//        this.$set(_index,'show',this.isShow)
//        this.isShow = !this.isShow

        if(_index.show == undefined || _index.show == false) {
          this.$set(_index,'show',true)
        } else if(_index.show == true){
          this.$set(_index,'show',false)
        }
      }
    },
    filters: {
      percent (data) {
        if(data) {
          return (parseFloat(data) * 100).toFixed(2)
        } else {
          return 0
        }
      },
      toFixed (data) {
        if (data) {
          return (parseFloat(data)).toFixed(2)
        } else {
          return 0
        }
      },
      num (data) {
        if(data == 0) {
          return 0
        }else{
          return (data*100).toFixed(2)
        }
      }
    }
  }
</script>
